package com.szm.mvc.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import com.szm.business.util.SysConstant;
import com.szm.mvc.util.bean.ActionBean;
import com.szm.mvc.util.bean.InterceptorBean;
import com.szm.mvc.util.bean.PackageBean;
import com.szm.mvc.util.bean.ResultsBean;

/**
 *@Author szm
 *@ClassName ConfigManager.java
 *@Description 读取配置文件，初始化 Map 存放至ServletContext
 *@Date 2015年11月25日
 */
public class ConfigManager {

	public Map<String,PackageBean> packageBeanMap= new HashMap<String,PackageBean>();
	
	public Map<String,InterceptorBean> interceptorBeanMap= new HashMap<String,InterceptorBean>();

	public ConfigManager() {
		
	}
	
	@SuppressWarnings("unchecked")
	public void init(File file){
		try {
			//获取XML document
			Document xmlDocument = XMLRead.getDocument(file);
			//Document xmlDocument = XMLRead.getDocumentByPath("config/struts/struts.xml");
			//package 包遍历
			List<Element> packageElementList =  XMLRead.getElements(xmlDocument, Constant.PACKAGE);
			if(null == packageElementList){
				return;
			}
			for(Element element : packageElementList){
				PackageBean packageBean = new PackageBean();
				packageBean.setName(XMLRead.getElementValue(element, Constant.NAME));
				packageBean.setNamespace(XMLRead.getElementValue(element, Constant.NAMESPACE));
				//设置默认命名空间
				/*if(null == packageBean.getNamespace() || "".equals(packageBean.getNamespace())){
					packageBean.setNamespace("/");
				}*/
				
				//global-results  全局results
				String xPath = "//"+Constant.STRUTS+"/"+Constant.PACKAGE+"[@"+Constant.NAME+"='"+packageBean.getName()+"']/"+Constant.GLOBAL_RESULTS+"/"+Constant.RESULT;
				List<Node> globalResultNodeList =  XMLRead.selectNodes(xmlDocument, xPath);
				if(null != globalResultNodeList){
					List<ResultsBean> globalResults = new ArrayList<ResultsBean>();
					for(Node globalResultNode : globalResultNodeList){
						ResultsBean resultsBean = new ResultsBean();
						resultsBean.setName(globalResultNode.valueOf("@"+Constant.NAME));
						resultsBean.setType(globalResultNode.valueOf("@"+Constant.TYPE));
						if(null != resultsBean.getType() && "" != resultsBean.getType() && Constant.JOSON.equals(resultsBean.getType())){
							List<Node> paramNodeList = globalResultNode.selectNodes(Constant.PARAM);
							if(null != paramNodeList){
								Map param = resultsBean.getParam();
								for(Node paramNode : paramNodeList){
									param.put(paramNode.valueOf("@"+Constant.NAME), paramNode.getStringValue());
								}
							}
						}else{
							resultsBean.setUrl(globalResultNode.getStringValue());
						}
						globalResults.add(resultsBean);
					}
					// package 存放默认返回值
					packageBean.setGlobalResults(globalResults);
				}
				
				//interceptors 拦截器
				
				//action 
				xPath = "//"+Constant.STRUTS+"/"+Constant.PACKAGE+"[@"+Constant.NAME+"='"+packageBean.getName()+"']/"+Constant.ACTION;
				List<Node> actionNodeList =  XMLRead.selectNodes(xmlDocument, xPath);
				if(null != actionNodeList){
					Map<String,ActionBean> actionBeanMap= new HashMap<String,ActionBean>();
					for(Node actionNode : actionNodeList){
						ActionBean actionBean = new ActionBean();
						actionBean.setName(actionNode.valueOf("@"+Constant.NAME));
						actionBean.setMethod(actionNode.valueOf("@"+Constant.METHOD));
						actionBean.setClazz(actionNode.valueOf("@"+Constant.CLAZZ));
						// action 设置 命名空间
						actionBean.setPackageBean(packageBean);
						//action  设置result 
						List<Node> resultNodeList = actionNode.selectNodes(Constant.RESULT);
						if(null != resultNodeList){
							List<ResultsBean> actionResults = new ArrayList<ResultsBean>();
							for(Node resultNode : resultNodeList){
								ResultsBean resultsBean = new ResultsBean();
								resultsBean.setName(resultNode.valueOf("@"+Constant.NAME));
								resultsBean.setType(resultNode.valueOf("@"+Constant.TYPE));
								if(null != resultsBean.getType() && "" != resultsBean.getType() && Constant.JOSON.equals(resultsBean.getType())){
									List<Node> paramNodeList = resultNode.selectNodes(Constant.PARAM);
									if(null != paramNodeList){
										Map param = resultsBean.getParam();
										for(Node paramNode : paramNodeList){
											param.put(paramNode.valueOf("@"+Constant.NAME), paramNode.getStringValue());
										}
									}
								}else{
									resultsBean.setUrl(resultNode.getStringValue());
								}
								actionResults.add(resultsBean);
							}
							actionBean.setResultsBeanList(actionResults);
							actionBeanMap.put(actionBean.getName(), actionBean);
						}
					}
					// package 设置 action
					packageBean.setActionBeanMap(actionBeanMap);
				}
				//存放 package 
				packageBeanMap.put(packageBean.getName(), packageBean);
			}
			
		} catch (Exception e) {
			System.out.println(SysConstant.SYS_FAIL+"初始化XML异常:"+e.getMessage());
		}
		
	}
	
	public static void main(String[] args) {
		ConfigManager configManager = new ConfigManager();
		configManager.init(null);
		System.out.println(configManager.packageBeanMap);
	}
	
}
